package com.mixpanel.android.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class StringUtils {

    /**
     * Static utility method to convert an InputStream to a String
     *
     * @param stream the InputStream to convert to a String
     * @return the converted String
     */

    public static String inputStreamToString(final InputStream stream) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();
        return sb.toString();
    }

    /**
     * 输出流转化为字符串
     *
     * @param output
     * @return
     */
    public static String readOutputStreamToString(OutputStream output) {
        ByteArrayOutputStream baos = null;
        ByteArrayInputStream swapStream = null;
        String result = null;
        try {
            baos = new ByteArrayOutputStream();
            baos = (ByteArrayOutputStream) output;
            swapStream = new ByteArrayInputStream(baos.toByteArray());
            result = swapStream.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
